package com.ext.aic.utils;

import android.text.TextUtils;
import android.view.accessibility.AccessibilityNodeInfo;

import com.ext.aic.beans.box.ViewElement;
import com.eyinfo.android_pure_utils.events.Action1;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class AccessibilityNodeUtils {

    private static volatile AccessibilityNodeUtils instance;

    public static AccessibilityNodeUtils getInstance() {
        if (instance == null) {
            synchronized (AccessibilityNodeUtils.class) {
                if (instance == null) {
                    instance = new AccessibilityNodeUtils();
                }
            }
        }
        return instance;
    }

    private List<AccessibilityNodeInfo> nodesWithType = new ArrayList<>();
    private ViewElement currentConversationListElement;

    /**
     * 根据类名查找节点
     * 不建议直接使用，推荐对应版本修复场景下使用
     *
     * @param node            AccessibilityNodeInfo
     * @param targetClassName 目标类名
     * @return AccessibilityNodeInfo
     */
    public AccessibilityNodeInfo findNodeByType(AccessibilityNodeInfo node, String targetClassName) {
        AccessibilityNodeInfo findNode = null;
        int childCount = node.getChildCount();
        for (int i = 0; i < childCount; i++) {
            AccessibilityNodeInfo childNode = node.getChild(i);
            String className = Objects.toString(childNode.getClassName(), "");
            if (TextUtils.equals(className, targetClassName)) {
                findNode = childNode;
                break;
            }
            int count = childNode.getChildCount();
            if (count > 0) {
                findNode = findNodeByType(childNode, targetClassName);
            }
        }
        return findNode;
    }

    public List<AccessibilityNodeInfo> findNodesByType(AccessibilityNodeInfo node, String targetClassName) {
        int childCount = node.getChildCount();
        for (int i = 0; i < childCount; i++) {
            AccessibilityNodeInfo childNode = node.getChild(i);
            String className = Objects.toString(childNode.getClassName(), "");
            if (className.contains(targetClassName)) {
                nodesWithType.add(childNode);
            } else {
                int count = childNode.getChildCount();
                if (count > 0) {
                    findNodesByType(childNode, targetClassName);
                }
            }
        }
        return nodesWithType;
    }

    public AccessibilityNodeInfo findNodeByText(AccessibilityNodeInfo node, String targetText) {
        AccessibilityNodeInfo findNode = null;
        int childCount = node.getChildCount();
        for (int i = 0; i < childCount; i++) {
            AccessibilityNodeInfo childNode = node.getChild(i);
            CharSequence text = childNode.getText();
            String textString = text == null ? "" : text.toString();
            if (textString.contains(targetText)) {
                findNode = childNode;
                break;
            }
            int count = childNode.getChildCount();
            if (count > 0 && findNode == null) {
                findNode = findNodeByText(childNode, targetText);
            }
        }
        return findNode;
    }

    public AccessibilityNodeInfo findNode(AccessibilityNodeInfo node, String viewId, String targetClassName, Action1<String> fuzzySearchCall) {
        List<AccessibilityNodeInfo> findNodes = node.findAccessibilityNodeInfosByViewId(viewId);
        if (!ObjectJudge.isNullOrEmpty(findNodes)) {
            return findNodes.get(0);
        }
        AccessibilityNodeInfo findNode = findNodeByType(node, targetClassName);
        if (findNode != null && fuzzySearchCall != null) {
            String viewIdResourceName = findNode.getViewIdResourceName();
            if (!TextUtils.isEmpty(viewIdResourceName)) {
                fuzzySearchCall.call(viewIdResourceName);
            }
        }
        return findNode;
    }

    public String verifyId(AccessibilityNodeInfo node, List<ViewElement> elements) {
        for (ViewElement element : elements) {
            List<AccessibilityNodeInfo> nodes = node.findAccessibilityNodeInfosByViewId(element.getId());
            if (!ObjectJudge.isNullOrEmpty(nodes)) {
                return element.getId();
            }
        }
        return "";
    }

    public List<AccessibilityNodeInfo> findNodesById(AccessibilityNodeInfo node, List<ViewElement> elements) {
        if (currentConversationListElement == null) {
            for (ViewElement element : elements) {
                List<AccessibilityNodeInfo> nodes = node.findAccessibilityNodeInfosByViewId(element.getId());
                if (!ObjectJudge.isNullOrEmpty(nodes)) {
                    currentConversationListElement = element;
                    return nodes;
                }
            }
        } else {
            return node.findAccessibilityNodeInfosByViewId(currentConversationListElement.getId());
        }
        return new ArrayList<>();
    }
}
